package com.gowants.pems.controller;

import com.gowants.pems.aspect.Importance;
import com.gowants.pems.aspect.OperType;
import com.gowants.pems.aspect.ScmsAopLog;
import com.gowants.pems.config.AccessLimit;
import com.gowants.pems.model.SysPermission;
import com.gowants.pems.service.SysPermissionService;
import com.gowants.pems.util.Const;
import com.gowants.pems.util.DateUtil;
import com.gowants.pems.util.JSONMap;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping(value = "/sysPermission/")
public class SysPermissionController extends BaseController {

	@Autowired
	private SysPermissionService scmsPermissionService;

	/**
	 * 查询列表
	 * 
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "getList")
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
	@ScmsAopLog(description = "菜单列表查看", opType = OperType.execute, importance = Importance.commonly)
	public JSONMap getList(String request, HttpServletRequest req) {
		String code = "";
		try {
			if (!isPower("/sysPermission/getList.do")) {
				return doResponse(req, "TSMS_0000228");
			}
			SysPermission record = new SysPermission();

			if (!"".equals(request) && null != request) {
				record = getRequestBody(request, SysPermission.class);
			}

			List<SysPermission> list = scmsPermissionService.select(record);
			return doResponse(req, code, list);
		} catch (Exception e) {
			code = e.getMessage();
			return doResponse(req, code, null);
		}
	}

	/**
	 * 保存
	 * 
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "save")
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
	@ScmsAopLog(description = "菜单添加", opType = OperType.execute, importance = Importance.commonly)
	public JSONMap save(String request, HttpServletRequest req) {
		String code = "TSMS_0000036";
		try {
			if (!isPower("/sysPermission/save.do")) {
				return doResponse(req, "TSMS_0000228");
			}
			SysPermission record = new SysPermission();

			if (!"".equals(request) && null != request) {
				record = getRequestBody(request, SysPermission.class);
			}
			String id = UUID.randomUUID().toString();
			if(StringUtils.isNotBlank(record.getUrl())){
				record.setUrl(record.getUrl().trim());
			}
			record.setId(id);
			record.setCreateUid(getUserId());
			record.setCreateTime(DateUtil.getDate());

			int i = scmsPermissionService.save(record);
			if (i > 0) {
				code = "TSMS_0000001";
			}

			return doResponse(req, code, null);
		} catch (Exception e) {
			code = e.getMessage();
			return doResponse(req, code, null);
		}
	}

	/**
	 * 删除
	 * 
	 * @param request {"id":""}
	 * @param req
	 * @return
	 * @remake
	 */
	@RequestMapping(value = "delete")
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
	@ScmsAopLog(description = "菜单删除", opType = OperType.execute, importance = Importance.important)
	public JSONMap delete(String request, HttpServletRequest req) {
		String code = "TSMS_0000048";
		try {
			if (!isPower("/sysPermission/delete.do")) {
				return doResponse(req, "TSMS_0000228");
			}
			JSONMap requestBody = getRequestBody(request);
			String id = requestBody.getString("id");

			int i = scmsPermissionService.deleteAll(id);
			if (i > 0) {
				code = "TSMS_0000050";
			}
		} catch (Exception e) {
			code = e.getMessage();
		}

		return doResponse(req, code, null);
	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param req
	 * @return
	 * @remake
	 */
	@RequestMapping(value = "update")
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
	@ScmsAopLog(description = "菜单修改", opType = OperType.execute, importance = Importance.important)
	public JSONMap update(String request, HttpServletRequest req) {
		String code = "TSMS_0000053";
		try {
			if (!isPower("/sysPermission/update.do")) {
				return doResponse(req, "TSMS_0000228");
			}
			SysPermission record = new SysPermission();

			if (!"".equals(request) && null != request) {
				record = getRequestBody(request, SysPermission.class);
			}
			//判断选择的是否是自己
        	if(record.getId().equals(record.getPid())) {
        		return doResponse(req, "TSMS_0000401", null);
        	}
			record.setUpdateTime(DateUtil.getDate());
			record.setUpdateUid(getUserId());
			if(StringUtils.isNotBlank(record.getUrl())){
				record.setUrl(record.getUrl().trim());
			}
			int i = scmsPermissionService.update(record);
			if (i > 0) {
				code = "TSMS_0000002";
			}

		} catch (Exception e) {
			code = e.getMessage();
		}

		return doResponse(req, code, null);
	}

	/**
	 * 启用禁用
	 * 
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "isenable")
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
	@ScmsAopLog(description = "菜单启用禁用", opType = OperType.execute, importance = Importance.important)
	public JSONMap isenable(String request, HttpServletRequest req) {
		String code = "TSMS_0000153";
		try {
			if (!isPower("/sysPermission/isenable.do")) {
				return doResponse(req, "TSMS_0000228");
			}
			JSONMap requestBody = getRequestBody(request);
			String id = requestBody.getString("id");

			code = scmsPermissionService.isenable(id);

		} catch (Exception e) {
			code = e.getMessage();
		}

		return doResponse(req, code, null);
	}

	/**
	 * 查询所有子集 （启用的接口）
	 * 
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "selectALLChildren")
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
	public JSONMap selectALLChildren(String request, HttpServletRequest req) {
		String code = "";
		try {
		    SysPermission record = new SysPermission();

			if (!"".equals(request) && null != request) {
				record = getRequestBody(request, SysPermission.class);
			}
			record.setIsenable(1);
			record.setType(1);
			List<SysPermission> list = scmsPermissionService.selectALLChildren(record);
			return doResponse(req, code, list);
		} catch (Exception e) {
			code = e.getMessage();
			return doResponse(req, code, null);
		}
	}
	
	
	@RequestMapping(value = "uploadImage")
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
	@ScmsAopLog(description = "菜单图标上传", opType = OperType.execute, importance = Importance.important)
	public JSONMap uploadImage(MultipartFile myFile,  HttpServletRequest req) {
		String code = "";
		try {
			
			// 校验上传文件
			int checkResult = scmsPermissionService.uploadCheck(myFile, Const.icon_type, Const.icon_size);

			// 文件超出限制大小
			if (checkResult == -1) {
				return doResponse(req, "TSMS_0000025");
			}

			// 文件格式不对
			if (checkResult == 0) {
				return doResponse(req, "TSMS_0000026");
			}
			String path = scmsPermissionService.uploadNew(myFile, Const.icon_path, null, Const.icon_mapping, true, true);
            if("xxxxxxxxxx".equals(path))
            {
            	return doResponse(req, "TSMS_0000257");
            }
            else
			if ("".equals(path)) {
				return doResponse(req, "TSMS_0000089");
			}
			JSONMap resultMap = new JSONMap();
			resultMap.put("path", path);
			return doResponse(req, code, resultMap);
			
		} catch (Exception e) {
			code = e.getMessage();
			return doResponse(req, code, null);
		}
	}

	@RequestMapping(value = "uploadSysImage")
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
	public JSONMap uploadSysImage(MultipartFile myFile,  HttpServletRequest req) {
		String code = "";
		try {
			
			// 校验上传文件
			int checkResult = scmsPermissionService.uploadCheck(myFile, Const.sys_image_type, Const.sys_image_size);

			// 文件超出限制大小
			if (checkResult == -1) {
				return doResponse(req, "TSMS_0000025");
			}

			// 文件格式不对
			if (checkResult == 0) {
				return doResponse(req, "TSMS_0000026");
			}
			String path = scmsPermissionService.uploadNew(myFile, Const.sys_image_path, null, Const.sys_mapping, true, true);
            if("xxxxxxxxxx".equals(path))
            {
            	return doResponse(req, "TSMS_0000257");
            }
            else
			if ("".equals(path)) {
				return doResponse(req, "TSMS_0000089");
			}
			JSONMap resultMap = new JSONMap();
			resultMap.put("path", path);
			return doResponse(req, code, resultMap);
			
		} catch (Exception e) {
			code = e.getMessage();
			return doResponse(req, code, null);
		}
	}
}
